/**
 * author:wos
 * time:2024-1-4
 * title:新增，修改目录权限
 */
const {makeSqlUpdate, makeSqlInsert, makeSqlSelect} = require('../../../utils/makeSql'),
    {formatTime} = require("../../../utils/timestamp")

async function edit(ctx, next) {
    let sql = ''
    const body = Object.assign({}, ctx.request.body)
    const menuIds = body.menuIds
    delete body.menuIds
    //更新数据
    if (ctx.request.method === 'PUT') {
        //判断dict_type是否重复
        const rsLen = await global.query(`SELECT roleId FROM sys_role WHERE roleId <> ${body.roleId} AND roleName = '${body.roleName}'`)
        if (rsLen.length > 0){
            global.response(ctx, {
                "status": 200,
                "code": 500,
                "data": '',
                "msg": '角色名称已经被使用'
            })
            return
        }
        let ruleMenuSql = `delete from sys_role_menu where roleId = ${body.roleId};`
        for (let i in menuIds){
            ruleMenuSql += `insert into sys_role_menu (roleId, menuId) values (${body.roleId}, ${menuIds[i]});`
        }
        await global.query(ruleMenuSql)

        const updateSqlConfig = {
            tableName: "sys_role",
            noUpdateKey: ['roleId', 'createBy', 'createTime', 'version'],
            autoUpdate: {"updateBy": ctx.username, "updateTime": formatTime()},
            where: ["version", 'roleId']
        }
        sql = makeSqlUpdate(body, updateSqlConfig)


        try {
            if (!sql) {
                global.response(ctx, {
                    "status": 200,
                    "code": 500,
                    "data": rs,
                    "msg": '参数错误'
                })
                return
            }
            let rsUpdate = await global.query(sql)
            global.response(ctx, {
                "status": 200,
                "code": 200,
                "data": rsUpdate,
                "msg": 'success'
            })
        } catch (err) {
            global.serverError(ctx)
        }

    //新建数据
    } else if (ctx.request.method === 'POST') {
        //判断dict_type是否重复
        const rsLen = await global.query(`SELECT roleId FROM sys_role WHERE roleName = '${body.roleName}'`)
        if (rsLen.length > 0){
            global.response(ctx, {
                "status": 200,
                "code": 500,
                "data": '',
                "msg": '角色名称同级已经被使用'
            })
            return
        }

        const insertSqlConfig = {
            tableName: "sys_role",
            autoUpdate: {"createBy": ctx.username, "createTime": formatTime()},
        }
        sql = makeSqlInsert(body, insertSqlConfig)


        try {
            if (!sql) {
                global.response(ctx, {
                    "status": 200,
                    "code": 500,
                    "data": rs,
                    "msg": '参数错误'
                })
                return
            }
            let rsInsert = await global.query(sql)
            let ruleMenuSql = ``
            for (let i in menuIds){
                ruleMenuSql += `insert into sys_role_menu (roleId, menuId) values (${rsInsert.insertId}, ${menuIds[i]});`
            }
            await global.query(ruleMenuSql)

            global.response(ctx, {
                "status": 200,
                "code": 200,
                "data": rsInsert,
                "msg": 'success'
            })
        } catch (err) {
            global.serverError(ctx)
        }
    } else {
        global.serverError(ctx)
    }

}



module.exports = edit